﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Collections.ObjectModel;



using HA.Data;

namespace HA.ModFlow.Controls.Windows.MapData
{
    /// <summary>
    /// winCoverageAttributeTable.xaml 的交互逻辑
    /// </summary>
    public partial class winCoverageAttributeTable : Window
    {
        public winCoverageAttributeTable(Coverage c)
        {
            InitializeComponent();


           

            //this.dgAttribute.ItemsSource = this._features;

            this.GenerateColumns(c.Setup);
            

            this._attributeTable = new PCCoverageAtrributeTable(c);

            this.DataContext = this._attributeTable;


            this._selectedIds = new List<string>();


            ///默认显示全部的图形
            this.cbShow.SelectedIndex = 0;


            this.c = c;

          

        }
        private Coverage c;

        #region 外部接口


        PCCoverageAtrributeTable _attributeTable;

        public PCCoverageAtrributeTable AttributeTable
        {
            get { return _attributeTable; }
            set { _attributeTable = value; }
        }

       


        ListCollectionView _lcv;


        List<string> _selectedIds;

        public void SetSelectedId(IList<string> selectedIds)
        {
            this._selectedIds.Clear();

            this._selectedIds.AddRange(selectedIds);

            
 
        }


        /// <summary>
        /// 指示是否只显示选择的物体
        /// </summary>
        public bool IsShowSelected
        {

            set {

                if (value == true)
                {
                    this.cbShow.SelectedIndex = 1;

                }
                else
                {
                    this.cbShow.SelectedIndex = 0;
                }

            }
          
        }





        #endregion




        #region 内部方法


        protected void GenerateColumns(CoverageSetup setUp)
        {
            if (setUp.ScTypes[MAPSCType.LAYERRANGE]==true)
            {

                
            }

 
        }

        private bool IsShowItem(object item)
        {


            PCFeatureObject fo = (PCFeatureObject)item;


            //需要分别考虑多种情况,包括选择类型，实体类型，bc类型等

            if (this._attributeTable.CurrentFOType==FeatureObjectType.POINT)
            {
                bool b=fo.FeatureObject is PointFO;

                if (!b)
                {
                    return false;
                    
                }


            }

            if (this._attributeTable.CurrentFOType == FeatureObjectType.LINE)
            {
                bool b = fo.FeatureObject is LineFO;

                if (!b)
                {
                    return false;

                }


            }

            if (this._attributeTable.CurrentFOType == FeatureObjectType.POLYGON)
            {
                bool b = fo.FeatureObject is PolygonFO;

                if (!b)
                {
                    return false;

                }


            }

            

            ///是否考虑选择


            if (this.cbShow.SelectedIndex==0)
            {
                return true;
                
            }

            ///只显示选择了的实体
            if (this.cbShow.SelectedIndex==1)
            {


               

                if (this._selectedIds.Contains(fo.FeatureObject.Id))
                {
                    return true;

                }
                else
                {
                    return false;
                }
                

            }



            return true;


           


          


        }



        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {


            this._attributeTable.Confirm();
            this.DialogResult = true;

            this.Close();

        }


        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(lvFeatureObject.ItemsSource);

            _lcv.Filter = new Predicate<object>(IsShowItem);


            if (this._attributeTable.Features.Count > 0)
            {
                this.lvFeatureObject.SelectedIndex = 0;

            }
            else
            {
                //当没有任何实体时，就不用显示什么了

                this.tcContent.Visibility = Visibility.Collapsed;
            }
         

        }

        private void cbShow_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this._lcv!=null)
            {

                this._lcv.Refresh();
                
            }
          
        }

        private void cbFeatureType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this._lcv != null)
            {

                this._lcv.Refresh();

            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //winAttributeV2 win = new winAttributeV2(this.c);
            //win.ShowDialog();
        }

    }
}
